package com.xinqi.modules.chat.chat.friend.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.chat.chat.friend.domain.ChatFriendApplyMessageEntity;
import com.xinqi.modules.chat.chat.friend.dto.rsp.ChatFriendApplyMessageResultDTO;
import com.xinqi.modules.chat.chat.friend.manager.ChatFriendApplyMessageManager;
import com.xinqi.modules.chat.chat.friend.mapper.ChatFriendApplyMessageMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 申请加好友消息内容管理层实现
 *
 * @author: system
 * @date: 2023/01/12
 */
@Service
@RequiredArgsConstructor
public class ChatFriendApplyMessageManagerImpl extends BaseServiceImpl<ChatFriendApplyMessageMapper, ChatFriendApplyMessageEntity> implements ChatFriendApplyMessageManager {

    /**
     * 根据申请ID获取申请消息列表
     *
     * @param applyId
     * @return
     */
    @Override
    public List<ChatFriendApplyMessageResultDTO> findByApplyId(Long applyId) {
        return this.baseMapper.findByApplyId(applyId);
    }

    /**
     * 查询申请最新的一条回复消息
     *
     * @param applyId
     * @return
     */
    @Override
    public ChatFriendApplyMessageEntity selectNewFriendApplyMessageByApplyId(Long applyId) {
        LambdaQueryWrapper<ChatFriendApplyMessageEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatFriendApplyMessageEntity::getApplyId, applyId)
            .orderByDesc(ChatFriendApplyMessageEntity::getCreateTime)
            .last("LIMIT 1");
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 根据申请ID删除申请消息
     *
     * @param applyId
     */
    @Override
    public void deleteByApplyId(Long applyId) {
        LambdaQueryWrapper<ChatFriendApplyMessageEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatFriendApplyMessageEntity::getApplyId, applyId);
        this.baseMapper.delete(wrapper);
    }
}
